/**
 * DistrictService.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年8月14日
 */
package com.pshare.artemis.basic.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pshare.artemis.basic.bean.District;
import com.pshare.artemis.basic.mapper.DistrictMapper;
import com.pshare.framework.mapper.BaseMapper;
import com.pshare.framework.service.BaseService;
import com.pshare.tool.StaticMapStore;

@Service
@Transactional
public class DistrictService extends BaseService<Integer, District> {
    
	private static final Logger logger = Logger.getLogger(DistrictService.class);

	@Autowired
	private DistrictMapper districtMapper;

	@Override
	protected BaseMapper<Integer, District> getBaseMapper() {
		return districtMapper;
	}

    @SuppressWarnings("unchecked")
    public List<District> queryAll() {
        boolean bExist = StaticMapStore.getInstance().containsKey(StaticMapStore.Key.DISTRICT);
        if (bExist) {
            Object rs = StaticMapStore.getInstance().get(StaticMapStore.Key.DISTRICT);
            return (List<District>) rs;
        }
        return districtMapper.queryAll();
    }

    public String getDistrictName(Integer districtId) {
    	if (districtId == null) {
    		return null;
    	}
    	boolean bExist = StaticMapStore.getInstance().containsKey(StaticMapStore.Key.DISTRICTMAP);
        if (bExist) {
            Map<Integer, String> rsMap = (Map<Integer, String>) StaticMapStore.getInstance().get(StaticMapStore.Key.DISTRICTMAP);
            return rsMap.get(districtId) == null ? "" : rsMap.get(districtId);
        }
        return "";
    }
    
    public int getCityIdByDistrictId(int districtId) {
    	return districtMapper.getCityIdByDistrictId(districtId);
    }
    
    public District get(Integer id) {
    	return districtMapper.get(id);
    }
	
	public Integer findIdByName(String name, Integer cityId) {
		if (StringUtils.isBlank(name)) {
    		logger.warn("name is blank");
    		return null;
    	}
		if (cityId == null) {
			logger.warn("cityId is null");
			return null;
		}
    	return districtMapper.findIdByName(name, cityId);
	}
	
	public District findByName(String name, Integer cityId) {
		if (StringUtils.isBlank(name)) {
    		logger.warn("name is blank");
    		return null;
    	}
		if (cityId == null) {
			logger.warn("cityId is null");
			return null;
		}
    	return districtMapper.findByName(name, cityId);
	}

}
